package van.planifolia.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import van.planifolia.pojo.Account;
import van.planifolia.result.ComResult;
import van.planifolia.service.AccountService;
import van.planifolia.util.JWTTool;
import van.planifolia.vo.LoginVo;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/23 17:28
 */
@RestController
@RequestMapping("/Account")
public class AccountController {

    @Resource(name = "accountService")
    private AccountService accountService;

    @RequestMapping("/login")
    public ComResult accountLogin(@RequestBody LoginVo loginVo) {
        // 调用dao层进行登录验证
        Account account = accountService.getAccountByIdAndPwd(loginVo);
        // 查看是否有执行结果，也就是按照条件看看是不是有这个人
        if (account != null) {
            // 若查到了将返回的用户信息封装成token返回
            Map<String, String> userInfo = new HashMap<>();
            userInfo.put("userInfo", account.toString());
            return new ComResult(2000, "成功", JWTTool.generateToken(userInfo));
        } else {
            // 没查到则直接返回mull
            return new ComResult(5001, "登录失败", null);
        }
    }
}
